<?php

namespace app\index\controller;

use app\BaseController;
use think\facade\Db;

class Index extends BaseController
{
  public function activity_list()
  {
    $data = Db::table('activity')->select();
    if (!$data) {
      returnJson(-2, '查询失败！');
    }

    $data = $data->toArray();
    foreach ($data as $key => $value) {
      $banner = Db::table('activity_banner')->where('a_id', $value['id'])->select();
      $data[$key]['banner'] = $banner->toArray();
    }

    returnJson(0, '成功！', $data);
  }


  public function goods_list()
  {
    $request = request()->param();

    $c_id = request()->param("c_id");
    $name = request()->param("name");
    $where = [];
    if ($c_id){
      $where[] = ['category_id', '=', $c_id];
    }
    if ($name){
      $where[] = ['name', 'like', '%'.$name.'%'];
    }
    //分页查询
    $page = pageQuery($request);

    $goodsList = Db::table('shop_goods')->order('sort', 'asc')->where($where)->page($page['page'], $page['count'])->select();
    if (!$goodsList) {
      returnJson(-2, '查询失败！');
    }
    $goodsCount = Db::table('shop_goods')->where($where)->count(); //总条数

    $data = [
      'count' => $page['count'],
      'total' => $goodsCount,
      'rows' => $goodsList,
    ];
    returnJson(0, '成功！', $data);
  }


  public function goods_info()
  {
    $id = request()->param('id');
    if (!$id) {
      returnJson(-2, '参数错误');
    }
    $data = Db::table('shop_goods')->where('id', $id)->find();
    if (!$data) {
      returnJson(-2, '查询失败！');
    }
    $data['is_collect'] = 2;

    $user = json_decode(session('user'), true);
    if ($user) {
      $res = Db::table('shop_collect')->where('goods_id', $data['id'])->where('uid', $user['id'])->find();
      if ($res && $res['status'] == 1){
        $data['is_collect'] = 1;
      }
    }
    
    returnJson(0, '成功！', $data);
  }


public function add_order()
  {
    $car = request()->param('car');
    $addr_id = request()->param('addr_id');
    $a_id = request()->param('a_id');
    if (empty($car) || !is_array($car)) {
      returnJson(-2, '参数错误');
    }

    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $addr = Db::table('user_addr')->where('id', $addr_id)->where('user_id', $user['id'])->find();

    $goodsArr = [];
    $money = 0;
    foreach ($car as $key => $value) {
      $goods = Db::table('shop_goods')->where('id', $value['goods_id'])->find();
      if (!$goods) {
        returnJson(-2, '商品不存在或已下架！');
      }

      $money += $goods['act_price'] * $value['num'];

      $goodsArr[] = [
        'goods_id' => $goods['id'],
        'goods_name' => $goods['name'],
        'goods_img' => $goods['img'],
        'goods_price' => $goods['act_price'],
        'goods_num' => $value['num'],
      ];
    }
    $money = round($money, 2);
    
    $order = [
      'user_id' => $user['id'],
      'user_name' => $user['username'],
      'addr_id' => $addr['id'],
      'addr_name' => $addr['name'],
      'addr_mobile' => $addr['mobile'],
      'addr_text' => $addr['address'],
      'status' => 1,
      'money' => $money
    ];
    $order_id = Db::table('shop_order')->insertGetId($order);
    
    foreach ($goodsArr as $key => $value) {
      $goodsArr[$key]['order_id'] = $order_id;
    }

    $res = Db::table('shop_order_goods')->insertAll($goodsArr);
    
    if ($res && $order_id) {
      returnJson(0, '添加成功！', ['money'=>$money, 'order_id'=>$order_id]);
    }
    returnJson(-2, '添加失败！');
  }

  public function category_list()
  {
    $num = request()->param('num')?request()->param('num'):10;
    $categoryList = Db::table('shop_category')->order('id', 'asc')->limit($num)->select();
    $data = [
      'rows' => $categoryList,
    ];
    returnJson(0, '成功！', $data);
  }


  public function add_collect () 
  {
    $goods_id = request()->param('goods_id');
    $status = request()->param('status');
    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $data = [
      'goods_id' => $goods_id,
      'status' => $status,
      'uid' => $user['id'],
    ];

    $res = Db::table('shop_collect')->where('goods_id', $goods_id)->where('uid', $data['uid'])->find();
    if ($res){
      $data['id'] = $res['id'];
    }

    $update = Db::table('shop_collect')->replace()->insert($data);
    if ($update >= 0) {
      returnJson(0, '成功！');
    }
    returnJson(-2, '失败！');
  }




}
